﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Threading;
using ClientLib.Common;
using ClientLib.MonitorState2;

using Bus = WpfDataBingding.TaskServiceReference;
namespace ClientLib.Common
{
    class ThreadScan
    {
        #region  ////状态集合
        private Thread scanthread;
        private int scantime;
        #endregion

        #region////状态设置
        //public Thread ScanThread
        //{
        //    get { return scanthread; }
        //    set
        //    {
        //        if (scanthread != null)
        //        {
        //            if (scanthread.IsAlive)
        //            {
        //                scanthread.Abort();
        //            }
        //        }
        //        scanthread = value; }
        //}

        public int ScanTime
        {
            get { return scantime; }
            set { scantime = value; }
        }
        #endregion 


        public void ScanState()
        {
            Collection devCollection = (Collection)Application.Current.Properties["MonitorCollection"];
            while (true)
            {
                using (Bus.DataBusServiceClient client = new Bus.DataBusServiceClient())
                {
                    DeviceSign[] devArr = devCollection.GetDeviceIDList();
                    
                    DeviceDynamic device;

                    foreach (var item in devArr)
                    {
                        device = devCollection.GetDevice(item.dev_id,item.mon_id);

                        string []  statelist=device.GetStateName();
                        
                        DynamicState state;
                        Bus.AttributeValue attVal;

                        foreach(var stateitem in statelist)
                        {

                        Bus.DeviceState busquerystate = new Bus.DeviceState();
                        busquerystate.AgentID = device.Monitor_ID;
                        busquerystate.DevType = device.Dev_Type;
                        busquerystate.DevID = device.Device_ID;
                        busquerystate.Name = stateitem;
                        attVal = client.QueryStateTest(busquerystate);

                        if (attVal != null)
                        {
                            AttributeValue stateVal = new AttributeValue(attVal.Type, attVal.Value);

                            state = new DynamicState(stateitem, device.Device_ID,device.Monitor_ID, device.Dev_Type);
                            state.StateValue = stateVal;
                            device.ChangeStateValue(state);
                        }

                        }/////单个设备所以状态扫描
                       

                    }///所有设备集合扫描
                } ///服务端口开

                Thread.Sleep(scantime);
            }////线程循环
        }


        public void threadStart()
        {
            try
            {
                if (scanthread != null)
                {
                    if (scanthread.IsAlive)
                    {
                        scanthread.Abort();
                    }
                }
                
                scanthread = new Thread(ScanState);
                scanthread.Start();
            }
            catch (Exception e)
            {
                throw new Exception("Error Thread");
            }
        }
        public void threadStop()
        {
            try
            {
                scanthread.Abort();
            }
            catch (Exception e)
            {
                throw new Exception("Error Thread");
            }
        }
    }
}
